<?php
namespace app\index\logic;
use think\Db;

class Recharge
{
    public function getList($params)
    {
        $field = "id,total_fee,first,virtual_currency,point_gift,vipdays,selected,lable,lable_open,desc,desc2,en_lable,en_desc,en_desc2";


        $list = Db::table('pay_recharge_item')
            ->where('manage_id',request()->channelId)->where('quick_id',request()->quickAppId)
            ->where('delete_time','<',1)->where('state',1)
            ->field($field)->order('sort ASC,id ASC')
            ->select();

        if(empty($list))
        {
            $list = Db::table('pay_recharge_item')
                ->where('manage_id',0)->where('quick_id',request()->quickAppId)
                ->where('delete_time','<',1)->where('state',1)
                ->field($field)->order('sort ASC,id ASC')
                ->select();
        }

        $UserInfoModel = \think\facade\App::model('UserInfo', 'model');
        $uInfo = $UserInfoModel->getOne(['id'=>request()->userInfo['id']],'id as uid,nickname,book_coin,pay_times,vip_exp_time,sign_days',request()->quickAppId);
        
        $data = [];
        foreach ($list as $v)
        {
            if($uInfo['pay_times'] >= 1 && $v['first'] == 1)
            {
                continue;
            }

            $total_fee = round($v['total_fee']/100,2);

            if($v['vipdays'] >= 1)
            {
                if(strlen($v['desc']) > 1)
                {
                    $desc = $params['lang'] == 'en' ? $v['en_desc'] : $v['desc'];
                    $desc2 = $params['lang'] == 'en' ? $v['en_desc2'] : $v['desc2'];
                }else{
                    $desc = $params['lang'] == 'en' ? 'Free Reading '.$v['vipdays'].' Day' : $v['vipdays'].'天免费阅读';
                    $desc2 = $params['lang'] == 'en' ? 'Spending '.round($total_fee/$v['vipdays'],2).' yuan per day' : '每天花费'.round($total_fee/$v['vipdays'],2).'元';
                }

            }else{
                if(strlen($v['desc']) > 1)
                {
                    $desc = $params['lang'] == 'en' ? $v['en_desc'] : $v['desc'];
                    $desc2 = $params['lang'] == 'en' ? $v['en_desc2'] : $v['desc2'];
                }else{
					if($params['lang'] == 'en')
					{
						$desc = $v['point_gift'] > 0 ? $v['virtual_currency'].' Book currency+'.$v['point_gift'].' Book Point' : $v['virtual_currency'].' Book currency';
						$desc2 = $v['point_gift'] > 0 ?  'Get an extra '.round($v['point_gift']/100,2).' yuan as a gift' : '';
					}else{
						$desc = $v['point_gift'] > 0 ? $v['virtual_currency'].'书币+'.$v['point_gift'].'书点' : $v['virtual_currency'].'书币';
						$desc2 = $v['point_gift'] > 0 ? "多送".round($v['point_gift']/100,2).'元' : '';
					}
                    
                }
            }

            $data[] = [
                'total_fee'         => $total_fee,
                'virtual_currency'  => $v['virtual_currency'],
                'point_gift'        => $v['point_gift'],
                'vipdays'           => $v['vipdays'],
                'selected'          => $v['selected'],
                'lable'             => $params['lang'] == 'en' ? $v['en_lable'] : $v['lable'],
                'lable_open'        => $v['lable_open'],
                'desc'              => $desc,
                'desc2'             => $desc2,
                'id'                => $v['id']
            ];

        }

        return $data;
    }

}